"""Header_Information
------------------------------------
    File_Name: OracleDBPool
    Author: yangzhide
    Date: 2024/10/11
    Description: 
------------------------------------
"""

import cx_Oracle


class OracleDBPool(object):
    """返回一个oralce 连接池"""

    def __init__(self, config):
        args_list = ['user', 'password', 'min', 'max', 'increment', 'encoding']
        # 收集会话参数
        self.conn_args = {key: value for key, value in config.items() if key in args_list}

        # 创建dsn
        self.host, self.port = config['host'], config['port']
        if "service_name" in config.keys():
            dsn = cx_Oracle.makedsn(self.host, self.port, config['service_name'])
        else:
            dsn = cx_Oracle.makedsn(self.host, self.port, config['sid'])

        # 更新获取会话参数
        self.conn_args.update({'dsn': dsn})

    def get_pool(self):
        """
        description: 获取连接池
        """
        pool = cx_Oracle.SessionPool(**self.conn_args)
        return pool

    def get_conn(self):
        """
        获取一个连接会话
        """
        pool = self.get_pool()
        conn = pool.acquire()
        cursor = conn.cursor()
        return conn, cursor
